package com.boomsecret.utils;

import com.boomsecret.bean.PageCondition;
import org.assertj.core.util.Lists;

import java.util.List;

/**
 * @author wangjinliang on 2018/12/3.
 */
public class PageUtils {
    private static void setStartIndex(PageCondition pageCondition) {
        int startIndex = (pageCondition.getPageNum() - 1) * pageCondition.getPageSize();
        pageCondition.setStartIndex(startIndex);
    }

    public static<T> List<T> getListByPageCondition(List<T> resultList, PageCondition pageCondition) {
        PageUtils.setStartIndex(pageCondition);
        if (pageCondition.getStartIndex() > resultList.size()) {
            return Lists.newArrayList();
        }
        int endIndex = pageCondition.getStartIndex() + pageCondition.getPageSize();
        if (endIndex > resultList.size()) {
            endIndex = resultList.size();
        }
        return resultList.subList(pageCondition.getStartIndex(), endIndex);
    }
}
